package com.func.domain.award.service;

import com.alibaba.fastjson.JSON;
import com.func.domain.award.event.SendAwardMessageEvent;
import com.func.domain.award.model.aggregate.UserAwardRecordAggregate;
import com.func.domain.award.model.entity.DistributeAwardEntity;
import com.func.domain.award.model.entity.TaskEntity;
import com.func.domain.award.model.entity.UserAwardRecordEntity;
import com.func.domain.award.model.valobj.TaskStateVO;
import com.func.domain.award.repository.IAwardRepository;
import com.func.domain.award.service.distribute.IDistributeAward;
import com.func.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * AwardService
 * </p>
 *
 * @author Simon
 * @version 2025.03.15.1.0.0
 * @description
 * @since 2025-03-15
 */

@Slf4j
@Service
public class AwardService implements IAwardService{

    @Resource
    private SendAwardMessageEvent sendAwardMessageEvent;
    @Resource
    private IAwardRepository awardRepository;
    @Resource
    private Map<String, IDistributeAward> distributeAwardMap;

    @Override
    public void saveUserAwardRecord(UserAwardRecordEntity userAwardRecordEntity) {
        // 构建消息对象
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage = new SendAwardMessageEvent.SendAwardMessage();
        sendAwardMessage.setAwardTitle(userAwardRecordEntity.getAwardTitle());
        sendAwardMessage.setOrderId(userAwardRecordEntity.getOrderId());
        sendAwardMessage.setUseId(userAwardRecordEntity.getUserId());
        sendAwardMessage.setAwardConfig(userAwardRecordEntity.getAwardConfig());
        sendAwardMessage.setAwardId(userAwardRecordEntity.getAwardId());

        BaseEvent.EventMessage<SendAwardMessageEvent.SendAwardMessage> sendAwardMessageEventMessage = sendAwardMessageEvent.buildEventMessage(sendAwardMessage);

        // 构建任务对象
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(userAwardRecordEntity.getUserId());
        taskEntity.setTopic(sendAwardMessageEvent.topic());
        taskEntity.setMessageId(sendAwardMessageEventMessage.getId());
        taskEntity.setMessage(sendAwardMessageEventMessage);
        taskEntity.setState(TaskStateVO.create);


        // 构建聚合对象
        UserAwardRecordAggregate userAwardRecordAggregate = UserAwardRecordAggregate.builder()
                .taskEntity(taskEntity)
                .userAwardRecordEntity(userAwardRecordEntity)
                .build();

        // 存储聚合对象
        awardRepository.saveAwardRecord(userAwardRecordAggregate);
    }

    @Override
    public void distributeAward(DistributeAwardEntity distributeAwardEntity) {
        log.info("发放奖品中, {}", JSON.toJSONString(distributeAwardEntity));
        String awardKey = awardRepository.queryAwardKey(distributeAwardEntity.getAwardId());
        if (null == awardKey) {
            log.error("分发奖品, 奖品ID不存在. awardKey: {}", awardKey);
            return ;
        }

        // 要分发的奖品
        IDistributeAward distributeAward = distributeAwardMap.get(awardKey);
        if (null == distributeAward){
            log.error("分发奖品, 对应的分发服务不存在 awardKey: {}" ,awardKey);
            throw new RuntimeException("分发奖品, 奖品 " + awardKey + " 对应的服务不存在");
        }

        // 发放奖品
        distributeAward.giveOutPrizes(distributeAwardEntity);
        log.info("发放奖品结束, {}", JSON.toJSONString(distributeAwardEntity));
    }
}
